package com.treeroot.planeservice.plane.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.treeroot.planeservice.plane.entity.PcPlane;
import com.treeroot.planeservice.plane.entity.PcPlaneTransfer;
import com.treeroot.planeservice.plane.mapper.PcPlaneTransferMapper;
import com.treeroot.planeservice.plane.service.PcPlaneTransferService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author:treeRoot
 * @Email:553503639@qq.com
 * @Date: Create in 08:44 / 2022/6/6
 */
@Service
@Transactional
public class PcPlaneTransferServiceImpl extends ServiceImpl<PcPlaneTransferMapper, PcPlaneTransfer> implements PcPlaneTransferService {
    @Override
    public Map getTransferList(long current, long limit) {
        //  调用方法实现分页

        Page<PcPlaneTransfer> planePage = new Page<>(current, limit);
        //调用方法的时候，底层封装把分页所有数据封装到pagePlane对象里
        baseMapper.selectPage(planePage,null);
        long total = planePage.getTotal();
        List<PcPlaneTransfer> records = planePage.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return map;

    }

    @Override
    public Map getQueryTransferList(long current, long limit, PcPlaneTransfer plane) {

        Page<PcPlaneTransfer> planePage = new Page<>(current, limit);
        //调用方法的时候，底层封装把分页所有数据封装到pagePlane对象里
        LambdaQueryWrapper<PcPlaneTransfer> wrapper = new QueryWrapper<PcPlaneTransfer>().lambda()


                .eq(  //根据航班号（flightNumber），出发日期（startDateTime）查询
                        !StringUtils.isEmpty(plane.getFlightNumber()),
                        PcPlaneTransfer::getFlightNumber,
                        plane.getFlightNumber()
                )
                .or(i -> i.eq(
                                !StringUtils.isEmpty(plane.getStartDateTime()),
                                PcPlaneTransfer::getStartDateTime,
                                plane.getStartDateTime()
                        )
                )

                .eq(//    出发日期(startDateTime)，出发地(startCity)、目的地(endCity) 查询
                        !StringUtils.isEmpty(plane.getStartDateTime()),
                        PcPlaneTransfer::getStartDateTime,
                        plane.getStartDateTime()
                )
                .or(i -> i.eq
                                (!StringUtils.isEmpty(plane.getStartDateTime()),
                                        PcPlaneTransfer::getStartDateTime,
                                        plane.getStartDateTime()

                                ).eq(!StringUtils.isEmpty(plane.getStartCity()),
                                PcPlaneTransfer::getStartCity,
                                plane.getStartCity()
                        ).eq(!StringUtils.isEmpty(plane.getEndCity()),
                                PcPlaneTransfer::getEndCity,
                                plane.getEndCity())
                );


        //    调用方法实现查询
        baseMapper.selectPage(planePage, wrapper);
        long total = planePage.getTotal();
        List<PcPlaneTransfer> records = planePage.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return map;
    }

}
